﻿using UnityEngine;

[ExecuteInEditMode, RequireComponent(typeof(UIButton))]
public class FunOpenBtn : MonoBehaviour
{
    /// <summary>
    /// 新手指引id
    /// </summary>
    public int GuideID;
    /// <summary>
    /// 功能没有开启的时候是否显示按钮
    /// </summary>
    public bool funNoOpenIsShow;
    /// <summary>
    /// 错误码id
    /// </summary>
    public int errorID;
    public UILabel txt1;
    public UILabel txt2;

    private UIButton btn;
    private bool funIsOpen;
    private string normalSpName;
    public string noOpenSpName;

    private void Awake()
    {
        btn = GetComponent<UIButton>();
        normalSpName = btn.normalSprite;
        btn.pixelSnap = false; OnUpdate(null);
        UIEventListener.Get(gameObject).onClick = BtnClick;
    }

    private void OnDestroy()
    {
        
    }

    private void OnUpdate(ZEvent ev)
    {
        if (funIsOpen == false)
        {
            Utility.SetActive(gameObject, funNoOpenIsShow);
            if (funNoOpenIsShow)
            {
                btn.normalSprite = noOpenSpName;
            }
        }
        else
        {
            btn.normalSprite = normalSpName;
            Utility.SetActive(gameObject, true);
        }
        SetTxtShow();
    }

    private void SetTxtShow()
    {
        if (funIsOpen == false)
        {
            if (funNoOpenIsShow)
            {
                Utility.SetActive(txt1, false);
                Utility.SetActive(txt2, true);
            }
        }
        else
        {
            Utility.SetActive(txt1, true);
            Utility.SetActive(txt2, false);
        }
    }

    private void BtnClick(GameObject go)
    {
        UIEventListener uiEvent = gameObject.AddMissingComponent<UIEventListener>();
        uiEvent.enabled = funIsOpen;

        if (!funIsOpen)
        {
            //错误码提示
        }
    }
}